"""
管理页面模板

提供简单的HTML模板响应
"""

from fastapi import Request
from fastapi.responses import HTMLResponse
from typing import Dict, Any


def get_template_response(template_name: str, context: Dict[str, Any] = None, request: Request = None) -> HTMLResponse:
    """获取模板响应"""
    if context is None:
        context = {}
    
    # 简单的HTML模板
    templates = {
        "admin_dashboard.html": get_dashboard_template(context),
        "user_list.html": get_user_list_template(context),
        "user_form.html": get_user_form_template(context),
        "login.html": get_login_template(context),
    }
    
    html_content = templates.get(template_name, get_default_template(template_name, context))
    return HTMLResponse(content=html_content)


def get_dashboard_template(context: Dict[str, Any]) -> str:
    """管理面板模板"""
    return f"""
    <!DOCTYPE html>
    <html>
    <head>
        <title>WebDAV 管理面板</title>
        <meta charset="utf-8">
        <style>
            body {{ font-family: Arial, sans-serif; margin: 20px; }}
            .header {{ background: #f4f4f4; padding: 20px; border-radius: 5px; }}
            .nav {{ margin: 20px 0; }}
            .nav a {{ margin-right: 20px; text-decoration: none; color: #007bff; }}
            .card {{ border: 1px solid #ddd; padding: 20px; margin: 10px 0; border-radius: 5px; }}
        </style>
    </head>
    <body>
        <div class="header">
            <h1>WebDAV 服务管理面板</h1>
        </div>
        <div class="nav">
            <a href="/admin/">首页</a>
            <a href="/admin/users">用户管理</a>
            <a href="/admin/status">服务状态</a>
            <a href="/docs">API文档</a>
        </div>
        <div class="card">
            <h3>服务状态</h3>
            <p>状态: {context.get('status', '运行中')}</p>
            <p>用户数: {context.get('user_count', 0)}</p>
            <p>WebDAV地址: <a href="/dav/">/dav/</a></p>
        </div>
        <div class="card">
            <h3>快速操作</h3>
            <a href="/admin/users/create">创建用户</a> |
            <a href="/admin/users">管理用户</a>
        </div>
    </body>
    </html>
    """


def get_user_list_template(context: Dict[str, Any]) -> str:
    """用户列表模板"""
    users = context.get('users', [])
    user_rows = ""
    for user in users:
        user_rows += f"""
        <tr>
            <td>{user.get('id', '')}</td>
            <td>{user.get('username', '')}</td>
            <td>{user.get('email', '')}</td>
            <td>{'是' if user.get('is_active') else '否'}</td>
            <td>
                <a href="/admin/users/{user.get('id')}/edit">编辑</a> |
                <a href="/admin/users/{user.get('id')}/delete" onclick="return confirm('确定删除?')">删除</a>
            </td>
        </tr>
        """
    
    return f"""
    <!DOCTYPE html>
    <html>
    <head>
        <title>用户管理 - WebDAV</title>
        <meta charset="utf-8">
        <style>
            body {{ font-family: Arial, sans-serif; margin: 20px; }}
            table {{ border-collapse: collapse; width: 100%; }}
            th, td {{ border: 1px solid #ddd; padding: 8px; text-align: left; }}
            th {{ background-color: #f2f2f2; }}
            .nav {{ margin: 20px 0; }}
            .nav a {{ margin-right: 20px; text-decoration: none; color: #007bff; }}
        </style>
    </head>
    <body>
        <div class="nav">
            <a href="/admin/">返回首页</a>
            <a href="/admin/users/create">创建用户</a>
        </div>
        <h1>用户管理</h1>
        <table>
            <tr>
                <th>ID</th>
                <th>用户名</th>
                <th>邮箱</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
            {user_rows}
        </table>
    </body>
    </html>
    """


def get_user_form_template(context: Dict[str, Any]) -> str:
    """用户表单模板"""
    user = context.get('user', {})
    is_edit = bool(user.get('id'))
    title = "编辑用户" if is_edit else "创建用户"
    action = f"/admin/users/{user.get('id')}/update" if is_edit else "/admin/users/create"
    
    return f"""
    <!DOCTYPE html>
    <html>
    <head>
        <title>{title} - WebDAV</title>
        <meta charset="utf-8">
        <style>
            body {{ font-family: Arial, sans-serif; margin: 20px; }}
            .form-group {{ margin: 15px 0; }}
            label {{ display: block; margin-bottom: 5px; }}
            input, select {{ width: 300px; padding: 8px; }}
            button {{ padding: 10px 20px; margin: 10px 5px 0 0; }}
            .nav {{ margin: 20px 0; }}
            .nav a {{ margin-right: 20px; text-decoration: none; color: #007bff; }}
        </style>
    </head>
    <body>
        <div class="nav">
            <a href="/admin/">返回首页</a>
            <a href="/admin/users">用户列表</a>
        </div>
        <h1>{title}</h1>
        <form method="post" action="{action}">
            <div class="form-group">
                <label>用户名:</label>
                <input type="text" name="username" value="{user.get('username', '')}" required>
            </div>
            {'<div class="form-group"><label>密码:</label><input type="password" name="password" required></div>' if not is_edit else ''}
            <div class="form-group">
                <label>邮箱:</label>
                <input type="email" name="email" value="{user.get('email', '')}">
            </div>
            <div class="form-group">
                <label>状态:</label>
                <select name="is_active">
                    <option value="true" {'selected' if user.get('is_active', True) else ''}>激活</option>
                    <option value="false" {'selected' if not user.get('is_active', True) else ''}>禁用</option>
                </select>
            </div>
            <div class="form-group">
                <label>读取权限:</label>
                <select name="can_read">
                    <option value="true" {'selected' if user.get('can_read', True) else ''}>允许</option>
                    <option value="false" {'selected' if not user.get('can_read', True) else ''}>禁止</option>
                </select>
            </div>
            <div class="form-group">
                <label>写入权限:</label>
                <select name="can_write">
                    <option value="true" {'selected' if user.get('can_write', True) else ''}>允许</option>
                    <option value="false" {'selected' if not user.get('can_write', True) else ''}>禁止</option>
                </select>
            </div>
            <button type="submit">保存</button>
            <button type="button" onclick="history.back()">取消</button>
        </form>
    </body>
    </html>
    """


def get_login_template(context: Dict[str, Any]) -> str:
    """登录模板"""
    error = context.get('error', '')
    
    return f"""
    <!DOCTYPE html>
    <html>
    <head>
        <title>管理员登录 - WebDAV</title>
        <meta charset="utf-8">
        <style>
            body {{ font-family: Arial, sans-serif; margin: 0; padding: 0; background: #f5f5f5; }}
            .login-container {{ max-width: 400px; margin: 100px auto; background: white; padding: 40px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }}
            .form-group {{ margin: 20px 0; }}
            label {{ display: block; margin-bottom: 5px; }}
            input {{ width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 3px; box-sizing: border-box; }}
            button {{ width: 100%; padding: 12px; background: #007bff; color: white; border: none; border-radius: 3px; cursor: pointer; }}
            button:hover {{ background: #0056b3; }}
            .error {{ color: red; margin: 10px 0; }}
        </style>
    </head>
    <body>
        <div class="login-container">
            <h2>管理员登录</h2>
            {f'<div class="error">{error}</div>' if error else ''}
            <form method="post" action="/admin/login">
                <div class="form-group">
                    <label>用户名:</label>
                    <input type="text" name="username" required>
                </div>
                <div class="form-group">
                    <label>密码:</label>
                    <input type="password" name="password" required>
                </div>
                <button type="submit">登录</button>
            </form>
        </div>
    </body>
    </html>
    """


def get_default_template(template_name: str, context: Dict[str, Any]) -> str:
    """默认模板"""
    return f"""
    <!DOCTYPE html>
    <html>
    <head>
        <title>WebDAV 管理</title>
        <meta charset="utf-8">
        <style>
            body {{ font-family: Arial, sans-serif; margin: 20px; }}
        </style>
    </head>
    <body>
        <h1>模板未找到: {template_name}</h1>
        <p>请检查模板配置</p>
        <a href="/admin/">返回首页</a>
    </body>
    </html>
    """